<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="所属排" prop="group.id"
                :rules="[
                  {required: true, message:'所属组不能为空', trigger:'blur'}
                 ]">
          <SelectTree
                      ref="group"
                      :props="{
                          value: 'id',             // ID字段名
                          label: 'name',         // 显示名称
                          children: 'childNodes'    // 子级字段名
                        }"

                      url="/smis/seat/jfSeatGroup/treeData"
                      :value="inputForm.group.id"
                      :clearable="true"
                      :accordion="true"
                      @getValue="(value) => {inputForm.group.id=value}"/>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="排序" prop="sort"
                :rules="[
                 ]">
              <el-input v-model="inputForm.sort" placeholder="请填写排序"     ></el-input>
           </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item label="座位编号" prop="name"
                :rules="[
                  {required: true, message:'座位编号不能为空', trigger:'blur'}
                 ]">
            <el-input v-model="inputForm.name" placeholder="请填写座位编号"     ></el-input>
          </el-form-item>
        </el-col> -->
        <el-col :span="12">
            <el-form-item label="开始编号" prop="nameStart"
                :rules="[
                 ]">
              <el-input v-model="inputForm.nameStart" placeholder="请填写开始编号"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="结束编号" prop="nameEnd"
                :rules="[
                 ]">
              <el-input v-model="inputForm.nameEnd" placeholder="请填写结束编号"     ></el-input>
           </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
            <el-form-item label="座位类型" prop="type"
                :rules="[
                 ]">
                <el-select v-model="inputForm.type" placeholder="请选择"  style="width: 100%;">
                          <el-option
                            v-for="item in $dictUtils.getDictList('lora_user_type')"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                      </el-select>
           </el-form-item>
        </el-col> -->
        
        <!-- <el-col :span="12">
            <el-form-item label="楼层" prop="floor.id"
                :rules="[
                  {required: true, message:'楼层不能为空', trigger:'blur'}
                 ]">
          <GridSelect
            title="选择楼层"
            labelName = 'name'
            labelValue = 'id'
            :value = "inputForm.floor.id"
            :limit="1"
            @getValue='(value) => {inputForm.floor.id=value}'
            :columns="[
            {
              prop: 'name',
              label: '名称'
            }
            ]"
            :searchs="[
            {
              prop: 'name',
              label: '名称'
            }
            ]"
            dataListUrl="/smis/floor/jfFloor/list"
            entityBeanName="jfFloor"
            queryEntityUrl="/smis/floor/jfFloor/queryById">
          </GridSelect>
           </el-form-item>
        </el-col> -->
        <el-col :span="12">
            <el-form-item label="楼层" prop="floor.id"
              :rules="[
              {required: true, message:'楼层不能为空', trigger:'blur'}
            ]">
          <SelectTree
            ref="floor"
            :props="{
                value: 'id',             // ID字段名
                label: 'showName',         // 显示名称
                children: 'childNodes'    // 子级字段名
              }"
            url="/smis/floor/jfFloor/treeData"
            :value="inputForm.floor.id"
            :clearable="true"
            :accordion="true"
            @getValue="(value) => {inputForm.floor.id=value; $refs.selectPoint.showFloor(value); inputForm.x = ''; inputForm.y = ''; $refs.selectPoint.removeMarkPoint();}"/>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
            <el-form-item label="X轴" prop="x"
                :rules="[
                  {required: true, message:'X轴不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.x" placeholder="请填写X轴"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="Y轴" prop="y"
                :rules="[
                  {required: true, message:'Y轴不能为空', trigger:'blur'}
                 ]">
              <el-input v-model="inputForm.y" placeholder="请填写Y轴"     ></el-input>
           </el-form-item>
        </el-col> -->
        <el-col :span="24" style="height: 400px;">
          <el-form-item label="所在位置" prop="x"
              :rules="[
                {required: true, message:'请选择所在位置', trigger:'blur'}
               ]">
            <select-point ref="selectPoint" @onMapClick="mapClick"></select-point>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  // import GridSelect from '@/components/gridSelect'
  import SelectPoint from '@/components/jfMap/selectPoint.vue'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          group: {
            id: ''
          },
          name: '',
          meshName: '',
          nameStart: '',
          nameEnd: '',
          type: '',
          sort: '',
          floor: {
            id: ''
          },
          x: '',
          y: ''
        }
      }
    },
    components: {
      SelectTree,
      // GridSelect,
      SelectPoint
    },
    methods: {
      init (method, id, searchForm) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建座位列表`
        } else if (method === 'edit') {
          this.title = '修改座位列表'
        } else if (method === 'view') {
          this.title = '查看座位列表'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          // this.$refs.inputForm.resetFields()
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/smis/seat/jfSeatList/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.jfSeatList)
              this.loading = false
              this.$refs.selectPoint.showFloorAndMarkPoint(this.inputForm.floor.id, this.inputForm.x, this.inputForm.y, this.inputForm.z || 0)
              // this.$refs.selectPoint.setMarkPoint(this.inputForm.floor.id, this.inputForm.x, this.inputForm.y)
            })
          } else {
            this.inputForm.group =  searchForm.group
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/smis/seat/jfSeatList/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
              this.loading = false
            })
          }
        })
      },
      mapClick (e) {
        this.inputForm.x = e.x
        this.inputForm.y = e.y
		this.inputForm.z = e.z
        // this.inputForm.meshName = e.name
        if(e.name.startsWith('jfdi') || e.name.startsWith('jflu') || e.name.startsWith('di') || e.name.startsWith('lu')) {
          this.$message({
            message: '当前选中的点，不在模型上',
            type: 'warning'
          });
          this.inputForm.meshName = ''
        } else {
          this.inputForm.meshName = e.name
        }
      }
    }
  }
</script>
